var couponTopic = {
    firstCancelChecked: [],//页面初始化一级分类选中的值
    secondCancelChecked: [],//页面初始化二级分类选中checked集合
    thirdCancelChecked: [],//页面初始化三级分类选中checked集合
    fourthCancelChecked: [],//页面初始化四级分类选中checked集合
    //交互回显
    firstCancel:[],
    secondCancel:[],
    thirdCancel:[],
    fourthCancel:[],
    // 保存接口入参
    firstNoClickIds: [],//一级类目的页面传参 没有点开二级的
    secondNoClickIds: [],//二级类目的页面传参 没有点开三级的
    thirdNoClickIds: [],//三级类目的页面传参 没有点开末级的
    firstCategoryIds: [],//一级类目的页面传参 点开二级的
    secondCategoryIds: [],// 二级类目的页面传参 点开三级的
    thirdCategoryIds: [],// 三级类目的页面传参 点开四级的
    categoryIds: [],// 末级类目的页面传参
    initDataFlag: false,//初始化数据是否有值标识
    selectBrandAry: [],//选择品牌集合
    initDataParams: {},// 初始化数据的参数
    timer :null,//定时器标识
    init: function () {
        this.getAreaListBySelectiveInfo()
        this.initPageInfo();
        this.addClick();

    },
    addClick: function () {
        // 点击保存
        $('#bottm').unbind().on('click', function () {
            var getNewArea = $("#getNewArea").find("option:selected").val();
            var params = {};
            var choiceBrandIds = [];
            var choiceBrandType = 1;//0 全部   1选择
            if (couponTopic.selectBrandAry.length > 0) {
                $.each(couponTopic.selectBrandAry, function (index, item) {
                    choiceBrandIds.push(item.id)
                });
                choiceBrandType = 1;
            } else {
                // 未选择品牌的时候默认选择全部
                $.each($('.input-checked'), function (index, item) {
                    var brandId = $(item).val();
                    choiceBrandIds.push(brandId);
                });
                choiceBrandType = 0
            }
            if (getNewArea == '') {
                layer.msg('请选择区域', {offset: 't'});
                return false;
            } else if (!$("input[name=applCate]:checked").val()) {
                layer.msg('请勾选适用品类', {offset: 't'});
                return false;
            } else if (couponTopic.firstCategoryIds.length <= 0 && couponTopic.firstNoClickIds.length <= 0) {
                layer.msg('请选择品类', {offset: 't'});
                return false;
            }
            if (couponTopic.initDataFlag) {// 初始化数据
                couponTopic.params.channelProductRule.addCategoryCodes = $('#addFourthCategory').val();//新增末级类codes
                couponTopic.params.channelProductRule.excludeCategoryCodes = $('#excludeFourthCategory').val();//删除末级类codes
                couponTopic.params.channelProductRule.limitCategoryType = $("input[name=xianzhi]:checked").val();
                couponTopic.params.channelProductRule.miniOrderType = $("input[name=qiding]:checked").val();
                couponTopic.params.channelProductRule.choiceBrandIds = choiceBrandIds.join(',');
                couponTopic.params.channelProductRule.choiceBrandType = choiceBrandType;
                params = couponTopic.params;
            } else {
                /*  console.log(couponTopic.firstCategoryIds, '一级点开的')
                  console.log(couponTopic.secondCategoryIds, '二级点开的')
                  console.log(couponTopic.thirdCategoryIds, '三级点开的')

                  console.log(couponTopic.firstNoClickIds, '一级没点开的')
                  console.log(couponTopic.secondNoClickIds, '二级没点开的')
                  // console.log(couponTopic.handleParamsFn(couponTopic.secondNoClickIds), '二级没点开的入参值ddd')
                  console.log(couponTopic.thirdNoClickIds, '三级没点开的')
                  console.log(couponTopic.categoryIds, '末级没点开的')*/
                // 新增保存入参
                /*   console.log($("#getNewArea").find("option:selected").attr('data-code'),'code值')*/
                params = {
                    channelProductRule: {
                        customerId: sessionStorage.getItem("customerid"),
                        customerName: sessionStorage.getItem("customerName"),
                        saleAreaCode: $("#getNewArea").find("option:selected").val(),// 后添加的区域code
                        saleAreaName: $("#getNewArea").find("option:selected").html(),
                        choiceCategoryType: $("input[name=applCate]:checked").val(),
                        firstCategoryIds: couponTopic.handleParamsFn(couponTopic.firstCategoryIds),
                        secondCategoryIds: couponTopic.handleParamsFn(couponTopic.secondCategoryIds),
                        thirdCategoryIds: couponTopic.handleParamsFn(couponTopic.thirdCategoryIds),
                        categoryIds: couponTopic.handleParamsFn(couponTopic.categoryIds),//四级类目
                        firstNoClickIds: couponTopic.firstNoClickIds.join(','),//一级类目的页面传参 没有点开二级的
                        secondNoClickIds: couponTopic.handleParamsFn(couponTopic.secondNoClickIds),//二级类目的页面传参 没有点开三级的
                        thirdNoClickIds: couponTopic.handleParamsFn(couponTopic.thirdNoClickIds),//三级类目的页面传参 没有点开末级的
                        addCategoryCodes: $('#addFourthCategory').val(),//新增末级类codes
                        excludeCategoryCodes: $('#excludeFourthCategory').val(),//删除末级类codes
                        limitCategoryType: $("input[name=xianzhi]:checked").val(),
                        miniOrderType: $("input[name=qiding]:checked").val(),
                        choiceBrandType: choiceBrandType,
                        choiceBrandIds: choiceBrandIds.join(',')
                    }
                }
            }
             console.log(params,'保存入参')
            couponTopic.saveInfo(params);
        })
        // 品牌搜索
        $('#brand-input').on('input', function () {
            var inputValue = $('#brand-input').val();
            couponTopic.delayFn(inputValue)
        });

        // 添加
        $('#add-brand').unbind().on('click', function () {
            var selectBrandLists = $('#selected-brand-lists li');
            var selectAry = [];
            $('.brand-lists').hide();
            // 获取所有选中的品牌
            $.each($('.input-checked'), function (index, item) {
                var brandId = $(item).val();
                var brandName = $(item).attr('data-brandName');
                if ($(item).is(":checked")) {
                    var obj = {
                        id: brandId,
                        name: brandName
                    }
                    selectAry.push(obj)
                }
            });
            selectAry.forEach(function (item) {
                if (JSON.stringify(couponTopic.selectBrandAry).indexOf(JSON.stringify(item)) < 0) {
                    couponTopic.selectBrandAry.push(item);
                }
            })
            if (couponTopic.selectBrandAry.length > 0 || selectBrandLists.length > 0) {
                $('#all-brand').hide();
                couponTopic.renderSelectedBrand(couponTopic.selectBrandAry)
            } else {
                $('#all-brand').show();
            }
        });
        // 删除已选品牌
        $('#selected-brand-lists').delegate('li .cancel-brand ', 'click', function () {
            var selectBrandLists = $('#selected-brand-lists li');
            var id = $(this).attr('data-id');
            $(this).parent().remove()
            couponTopic.selectBrandAry = couponTopic.selectBrandAry.filter(function (item) {
                return item.id != id
            });
            // 品牌全部的展示
            if (couponTopic.selectBrandAry.length > 0 || selectBrandLists.length > 0) {
                $('#all-brand').hide()
            } else {
                $('#all-brand').show()
            }

        })
        // 删除四级分类
        $('#cancel-fourth-classify').unbind().on('click', function () {
            sessionStorage.setItem("deleteFlag", 1);
            if ($('#excludeFourthCategory').val()) {
                sessionStorage.setItem("deleteSession", $('#excludeFourthCategory').val());
            }
            layer_show("请选择要删除的四级类编码", "editBrand.html?excludeFourthCategory=" + $('#excludeFourthCategory').val(), 669, 445);
        });
        //新增四级分类
        $('#add-fourth-classify').unbind().on('click', function () {
            sessionStorage.setItem("addFlag", 1);
            layer_show("请选择要增加的四级类编码", "editBrand.html?addFourthCategory=" + $('#addFourthCategory').val(), 669, 445);
        });
        // 取消按钮
        $('#cancel-button').unbind().on('click', function () {
            parent.layer.closeAll();
        })
        // 一级分类点击查询
        $('#classSet1').delegate('input', 'click', function () {
            var checkFlag = $(this).prop('checked');
            var categoryId = $(this).val();
            var level = $(this).attr('data-level');
            var parentUnit = $(this).attr('data-parentuuid');
            var obj = {};
            var threeItem = '';// 存储三级点击的Id
            var fourthItem = [];//存储四级点击的id值
            if (checkFlag) {
                var flag = couponTopic.clickClassifyNameFn(this, 1);
                // console.log(flag,'标识验证')
                if (flag) {
                    couponTopic.getSubClassifyInfo((Number(flag.level) + 1), flag.categoryId);
                    $('#classDiv3').hide();
                    $('#classDiv4').hide();
                    obj[parentUnit] = categoryId
                    couponTopic.firstCategoryIds.push(obj);
                }
            } else {
                couponTopic.secondCancel.forEach(function(item){
                    for(var key in item){
                        if(key == categoryId){
                            couponTopic.secondCancel.splice(couponTopic.secondCancel.indexOf(item),1)
                        }
                    }
                });
                $('#classDiv2').hide();
                $('#classDiv3').hide();
                $('#classDiv4').hide();
                // 一级点开的
                couponTopic.firstCategoryIds = couponTopic.firstCategoryIds.filter(function (item) {
                    for (var key in item) {
                        return item[key] != categoryId
                    }
                });
                // 删除二级分类对应的没有点开传值 secondNoClickIds
                couponTopic.secondNoClickIds = couponTopic.secondNoClickIds.filter(function (item) {
                    for (var key in item) {
                        return key != categoryId;
                    }
                });
                couponTopic.secondCategoryIds = couponTopic.secondCategoryIds.filter(function (item) {
                    for (var key in item) {
                        if (key == categoryId) {
                            threeItem = item[key];
                        }
                        return key != categoryId
                    }
                });
                // 处理三级没有点开的
                couponTopic.thirdNoClickIds = couponTopic.thirdNoClickIds.filter(function (item) {
                    for (var key in item) {
                        return threeItem.indexOf(key) <=-1
                    }
                })
                // 处理三级点开的
                couponTopic.thirdCategoryIds = couponTopic.thirdCategoryIds.filter(function (item) {
                    for (var key in item) {
                        if (threeItem.indexOf(key) > -1) {
                            fourthItem = fourthItem.concat(item[key])
                        } else {
                            return threeItem.indexOf(key) <=-1
                        }
                    }
                });
                // 处理末级没有点开传值
                couponTopic.categoryIds = couponTopic.categoryIds.filter(function (item) {
                    for (var key in item) {
                        return fourthItem.indexOf(key) <=-1;
                    }
                })
                //console.log(couponTopic.secondNoClickIds, '二级没有点开的')
            }
        });
        // 二级分类点击
        $('#classSet2').delegate('input', 'click', function () {
            couponTopic.clickInputClassifyFn(this, 2);
            var check = $(this).prop('checked')
            var parentUnit = $(this).attr('data-parentuuid');
            var categoryId = $(this).val();
            if(!check){
                couponTopic.cancelFn(parentUnit,categoryId,couponTopic.secondCancel);
                couponTopic.thirdCancel.forEach(function(item){
                    for(var key in item){
                        if(key == categoryId){
                            couponTopic.thirdCancel.splice(couponTopic.thirdCancel.indexOf(item),1)
                        }
                    }
                });
            }else{
                couponTopic.cancelCheckFn(parentUnit,categoryId,couponTopic.secondCancel)
            }
            $('#classDiv3').hide();
            $('#classDiv4').hide();
        });
        // 三级分类点击
        $('#classSet3').delegate('input', 'click', function () {
            couponTopic.clickInputClassifyFn(this, 3);
            var check = $(this).prop('checked')
            var parentUnit = $(this).attr('data-parentuuid');
            var categoryId = $(this).val();
            if(!check){
                couponTopic.cancelFn(parentUnit,categoryId,couponTopic.thirdCancel);
                couponTopic.fourthCancel.forEach(function(item){
                    for(var key in item){
                        if(key == categoryId){
                            couponTopic.fourthCancel.splice(couponTopic.fourthCancel.indexOf(item),1)
                        }
                    }
                });
            }else{
                couponTopic.cancelCheckFn(parentUnit,categoryId,couponTopic.thirdCancel)
            }
            $('#classDiv4').hide();
        });
        // 四级分类点击
        $('#classSet4').delegate('input', 'click', function () {
            couponTopic.clickInputClassifyFn(this, 4);
            var check = $(this).prop('checked')
            var parentUnit = $(this).attr('data-parentuuid');
            var categoryId = $(this).val();
            if(!check){
                couponTopic.cancelFn(parentUnit,categoryId,couponTopic.fourthCancel);
            }else{
                couponTopic.cancelCheckFn(parentUnit,categoryId,couponTopic.fourthCancel)
            }
        });
        /*点击文字获取下一级*/
        // 点击一级分类
        $('#classSet1').delegate('label', 'click', function () {
            var flag = couponTopic.clickClassifyNameFn(this, 1);
            var inputCheck = $(this).prev().is(':checked')
            if (flag && inputCheck) {
                couponTopic.getSubClassifyInfo((Number(flag.level) + 1), flag.categoryId);
                $('#classDiv3').hide();
                $('#classDiv4').hide();
            }
            //console.log(couponTopic.secondNoClickIds, '二级没有点开的')
        });
        // 点击二级分类
        $('#classSet2').delegate('label', 'click', function () {
            var flag = couponTopic.clickClassifyNameFn(this, 2);
            if (flag) {
                couponTopic.getSubClassifyInfo((Number(flag.level) + 1), flag.categoryId);
                $('#classDiv4').hide();
            }
        });
        // 点击三级分类
        $('#classSet3').delegate('label', 'click', function () {
            var flag = couponTopic.clickClassifyNameFn(this, 3);
            if (flag) {
                couponTopic.getSubClassifyInfo((Number(flag.level) + 1), flag.categoryId);
            }

        });
        // 全选
        $('#selectAll').unbind().on('click', function () {
            var selectInput = $('#classSet1').children().find('input');
            $.each(selectInput, function (index, item) {
                $(item).prop('checked', true);
                //全选一级类目没有点开二级类目的
                couponTopic.firstNoClickIds.push($(item).val());
            });


        });
        // 重置
        $('#selectReset').unbind().on('click', function () {
            if (couponTopic.initDataFlag) {
                return false
            }
            var unSelectInput = $('#classSet1').children().find('input');
            $.each(unSelectInput, function (index, item) {
                $(item).prop('checked', false)
            });
            $('#radio-7').prop('checked', false)
            couponTopic.firstCancelChecked = [];
            couponTopic.secondCancelChecked = [];
            couponTopic.thirdCancelChecked = [];
            couponTopic.fourthCancelChecked = [];
            $('#classDiv2').hide();
            $('#classDiv3').hide();
            $('#classDiv4').hide();
            // 清空参数集合
            couponTopic.firstCategoryIds = [];
            couponTopic.secondCategoryIds = [];
            couponTopic.thirdCategoryIds = [];
            couponTopic.categoryIds = [];
            couponTopic.firstNoClickIds = [];
            couponTopic.secondNoClickIds = [];
            couponTopic.thirdNoClickIds = [];
        });
        // 商品搜索弹窗取消
        $(document).bind('click', function (e) {
            if ($(e.target).closest(".brand-lists").length == 0 && $(e.target).closest(".brand-lists").length == 0) {
                $(".brand-lists").hide();
            }
        })
    },
    // 延迟执行函数
    delayFn: function (value) {
        clearTimeout(couponTopic.timer);
        couponTopic.timer = setTimeout(function () {
            couponTopic.getBrandInfo(value);
            $('.brand-lists').show();
        }, 1000)
    },
    // 区域
    getAreaListBySelectiveInfo: function () {
        let params = {}
        var successCallBack = function (data) {
            if (data.errorCode == 0) {
                let result = data.data;
                couponTopic.renderGetAreaList(result)
            } else {
                layer.msg('请求异常', {offset: 't'})
            }
        }
        return web.doAjax(root.dockingplatform.getSalesAreaList, 'post', params, successCallBack, 1);
    },
    // 渲染区域
    renderGetAreaList: function (data) {
        var str = '';
        if (data && data.length > 0) {
            $.each(data, function (index, item) {
                str += '<option value="' + item.areaCode + '" data-code="' + item.areaId + '">' + item.areaName + '</option>'
            });
            $('#getNewArea').html('<option value="0" selected>请选择区域</option>' + str)
        } else {
            var empty = '<option value="0" selected>请选择区域</option>' + '<option>暂无区域</option>'
            $('#getNewArea').html(empty)
        }
    },
    // 获取分类信息
    getSubClassifyInfo: function (level, categoryId) {
        var params = {
            levelNum: level,
            parentUuid: categoryId

        }
        var successCallBack = function (data) {
            if (data.retStatus == 1) {
                var result = data.retData;
                couponTopic.renderSubClassify(result, level, categoryId)
            } else {
                layer.msg(data.retMessage, {offset: 't'})
            }
        }
        return web.doAjax(root.goodsInter.goods_getProductCategory, 'post', params, successCallBack, 1);
    },
    renderSubClassify: function (data, level, categoryId) {
        var str = '';
        if (data && data.length > 0) {
            $.each(data, function (index, item) {
                if (level == 1) {
                    str += '<li class="removeEl3 clearfix pl-10 mb-5">' +
                        '<div class="f-l pd-5" >' +
                        '<input data-level="' + level + '" type="checkbox"  value="' + item.uuid + '"  data-parentuuid="' + item.parentUuid + '" class="checkbox" />' +
                        '<label class="pl-10" data-level="' + level + '" data-id="' + item.uuid + '" data-parentuuid="' + item.parentUuid + '">' + item.categoryNo + ' ' + item.categoryName + '</label>' +
                        '</div>' +
                        '</li>'
                } else {
                    str += '<li class="removeEl3 clearfix pl-10 mb-5">' +
                        '<div class="f-l pd-5" >' +
                        '<input data-level="' + level + '" type="checkbox" value="' + item.uuid + '" data-parentuuid="' + item.parentUuid + '" class="checkbox" checked="checked" />' +
                        '<label class="pl-10" data-level="' + level + '" data-id="' + item.uuid + '" data-parentuuid="' + item.parentUuid + '">' + item.categoryNo + ' ' + item.categoryName + '</label>' +
                        '</div>' +
                        '</li>'
                }
            });
        } else {
            str = '<li class="removeEl3 clearfix pl-10 mb-5">无数据</li>'
        }
        $("#classSet" + Number(level) + "").html(str);
        $("#classDiv" + Number(level) + "").show();
        if (level == 1) {
            couponTopic.renderSelectInput('classSet1', 1, couponTopic.initDataFlag)
        } else if (level == 2) {
            //console.log(couponTopic.secondNoClickIds,'couponTopic.secondNoClickIdscouponTopic.secondNoClickIds')
            couponTopic.notClickIdsFn('classSet2', couponTopic.secondNoClickIds, categoryId);
            couponTopic.renderSelectInput('classSet2', 2, couponTopic.initDataFlag)
            //console.log(couponTopic.secondNoClickIds,'kkkkk')
        } else if (level == 3) {
            // 三级类目的页面传参 没有点开末级的
            couponTopic.notClickIdsFn('classSet3', couponTopic.thirdNoClickIds, categoryId);
            couponTopic.renderSelectInput('classSet3', 3, couponTopic.initDataFlag);
            //console.log(couponTopic.thirdNoClickIds, '3')
        } else {
            //四级类目的页面传参
            couponTopic.notClickIdsFn('classSet4', couponTopic.categoryIds, categoryId);
            couponTopic.renderSelectInput('classSet4', 4, couponTopic.initDataFlag);
        }
    },

    // 存储没有点击的接口字段值
    /**
     *
     * @param ele 当前级别集合
     * @param notClickIdsAry 存储没有点击的数组
     * @param categoryId 当前操作的分类Id
     */
    notClickIdsFn: function (ele, notClickIdsAry, categoryId) {
        var ary = [];
        var obj = {};
        var clickArys = [];
        var classInput = $("#" + ele + "").children().find('input');
        $.each(classInput, function (index, item) {
            ary.push($(item).val())
        })
        obj[categoryId] = ary;
        //方法二
        var flag = notClickIdsAry.some(function (item) {
            return item.hasOwnProperty(categoryId)
        })
        if (!flag) {
            notClickIdsAry.push(obj)
        }

        /*  方法一
        $.each(notClickIdsAry,function(index,item){
              for(var key in item){
                  clickArys.push(key)
              }
          })
          if(clickArys.indexOf(categoryId) <=-1){
              notClickIdsAry.push(obj)
          }*/
    },
    // 存储点开的接口字段
    /**
     *
     * @param parentUnit  父级id
     * @param categoryIds 当前id
     * @param clickIdsAry 操作的数组
     */
    clickIdsFn: function (parentUnit, categoryIds, clickIdsAry) {
        var obj = {};
        var ary = [];
        ary.push(categoryIds)
        obj[parentUnit] = ary;
        if (clickIdsAry.length > 0) {
            clickIdsAry.forEach(function (item, index) {
                for (var key in item) {
                    if (key == parentUnit) {
                        if (item[key].indexOf(categoryIds) < 0) {
                            item[parentUnit].push(categoryIds)
                        }
                    } else {
                        clickIdsAry.push(obj)
                    }
                }
            })
        } else {
            clickIdsAry.push(obj)
        }
    },
    //存储取消对应的数据集合
    cancelFn:function(parentUnit, categoryIds, clickIdsAry){
        var obj = {};
        var ary = [];
        var saveAry = [];
        ary.push(categoryIds)
        obj[parentUnit] = ary;
        /*此处可以优化*/
        if (clickIdsAry.length > 0) {
            clickIdsAry.forEach(function (item, index) {
                for(var key in item){
                    saveAry.push(key)
                }
            })
            if(saveAry.indexOf(parentUnit) >-1){
                clickIdsAry.forEach(function (item, index) {
                      if(item[parentUnit]){
                          item[parentUnit].push(categoryIds)
                      }
                })
            }else{
                clickIdsAry.push(obj)
            }
        } else {
            clickIdsAry.push(obj)
        }
    },

    // 删除对应取消的数据
    /**
        @param parentUnit  父级id
     * @param categoryIds 当前id
     * @param clickIdsAry 操作的数组
     */
    cancelCheckFn:function(parentUnit,categoryId,clickIdsAry){
       clickIdsAry.forEach(function(item){
            for(var key in item){
                if(key == parentUnit){
                    if (item[key].indexOf(categoryId) > -1) {
                        item[key].splice(item[key].indexOf(categoryId), 1)
                    }
                }
            }
        })
    },

    // 初始化页面数据
    initPageInfo: function () {
        var params = {
            categoryName:'',
            customerId: sessionStorage.getItem("customerid")
        };
        var successCallBack = function (data) {
            if (data.errorCode == 0) {
                var result = data.data;
                couponTopic.getSubClassifyInfo(1, 'systemUuid');
                couponTopic.renderInitPage(result)
            } else {
                layer.msg(data.msg, {offset: 't'})
            }
        }
        return web.doAjax(root.dockingplatform.selectChannelProductRule, 'post', params, successCallBack);
    },
    renderInitPage: function (data) {
        if (!!data) {
            couponTopic.initDataFlag = true;
            var saleAreaCode = data.saleAreaCode;//区域code
            var saleAreaName = data.saleAreaName;//区域
            var choiceCategoryType = data.choiceCategoryType;//品类
            var choiceBrandType = data.choiceBrandType;// 品牌
            var choiceBrandIds = data.choiceBrandIds;//品牌集合
            var limitCategoryType = data.limitCategoryType;// 是否限制类
            var miniOrderType = data.miniOrderType;// 最小起订量
            /*分类*/
            var firstCategoryList = data.firstCategoryList;//一级分类
            var secondCategoryList = data.secondCategoryList;//二级分类
            var thirdCategoryList = data.thirdCategoryList;//三级分类
            var categoryList = data.categoryList;//末级分类
            // id集合
            var firstCategoryIds = data.firstCategoryIds;
            couponTopic.firstCategoryIds = data.firstCategoryIds;
            var secondCategoryIds = data.secondCategoryIds;
            var thirdCategoryIds = data.thirdCategoryIds;
            var categoryIds = data.categoryIds;

            /* 品牌*/
            var brandList = data.brandList ? data.brandList : [];
            $('#getNewArea').val(saleAreaCode);
            $("#getNewArea").attr("disabled", "disabled");//禁止选择
            if (choiceCategoryType == 1) {
                $("#radio-7").prop("checked", true);
            }
            //品牌
            if (choiceBrandType == 1) {
                //适用
                $("#box-2").prop("checked", true);
            } else if (choiceBrandType == 2) {
                //排除
                $("#box-3").prop("checked", true);
            } else {
                //不限
                $("#box-1").prop("checked", true);
            }
            // 是否限制类
            if (limitCategoryType == 1) {
                //适用
                $("#xz-2").prop("checked", true);
            } else if (limitCategoryType == 2) {
                //排除
                $("#xz-3").prop("checked", true);
            } else {
                //不限
                $("#xz-1").prop("checked", true);
            }
            // 最小起订量
            if (miniOrderType == 1) {
                //适用
                $("#qd-2").prop("checked", true);
            } else if (miniOrderType == 2) {
                //排除
                $("#qd-3").prop("checked", true);
            } else {
                //不限
                $("#qd-1").prop("checked", true);
            }
            // 分类数据的回显
            //一级分类
            if (firstCategoryList && firstCategoryList.length > 0) {
                $.each(firstCategoryList, function (index, item) {
                    couponTopic.firstCancelChecked.push(item.uuid.toString())
                })
            }
            //二级分类
            if (secondCategoryList && secondCategoryList.length > 0) {
                $.each(secondCategoryList, function (index, item) {
                    couponTopic.secondCancelChecked.push(item.uuid.toString())
                })
            }
            // 三级分类
            if (thirdCategoryList && thirdCategoryList.length > 0) {
                $.each(thirdCategoryList, function (index, item) {
                    couponTopic.thirdCancelChecked.push(item.uuid.toString())
                })
            }
            // 四级分类
            if (categoryList && categoryList.length > 0) {
                $.each(categoryList, function (index, item) {
                    couponTopic.fourthCancelChecked.push(item.uuid.toString())
                })
            }
            // 品牌数据重构回显回显
            if (brandList.length > 0) {
                $('#all-brand').hide();
                var ary = [];
                $.each(brandList, function (index, item) {
                    var obj = {
                        id: item.uuid,
                        name: item.brandName
                    };
                    ary.push(obj)
                });
                couponTopic.selectBrandAry = ary;
                couponTopic.renderSelectedBrand(ary)
            } else {
                $('#all-brand').show()
            }
            // 初始化数据入参
            couponTopic.params = {
                channelProductRule: {
                    customerId: sessionStorage.getItem("customerid"),
                    customerName: sessionStorage.getItem("customerName"),
                    saleAreaCode: saleAreaCode,
                    saleAreaName: saleAreaName,
                    choiceCategoryType: choiceCategoryType,
                    firstCategoryIds: firstCategoryIds,
                    secondCategoryIds: secondCategoryIds,
                    thirdCategoryIds: thirdCategoryIds,
                    categoryIds: categoryIds,
                    firstNoClickIds: '',
                    secondNoClickIds: '',
                    thirdNoClickIds: '',
                }
            }

            // 全选  重置按钮隐藏
            $('#hide').hide();
            $('#cancel-fourth-classify').show();
            $('#add-fourth-classify').show();
        } else {
            couponTopic.initDataFlag = false;
            $('#hide').show();
        }
    },
    // 获取子分类点击封装
    clickInputClassifyFn: function (eventTarget, level) {
        var checkFlag2 = $(eventTarget).prop('checked');
        var categoryId = $(eventTarget).val();
        var parentUnit = $(eventTarget).attr('data-parentuuid');
        var threeItem = '';
        if (!checkFlag2) {
            switch (level) {
                case 1:
                    return couponTopic.firstCancelChecked.push(categoryId);
                case 2:
                    // 删除二级分类没有点开传的值
                    couponTopic.secondNoClickIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                if (item[key].indexOf(categoryId) > -1) {
                                    item[key].splice(item[key].indexOf(categoryId), 1)
                                }
                            }
                        }
                    });
                    // 删除二级点开的
                    couponTopic.secondCategoryIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                if (item[key].indexOf(categoryId) > -1) {
                                    item[key].splice(item[key].indexOf(categoryId), 1)
                                }
                            }
                        }
                    });
                    // console.log(couponTopic.secondCategoryIds,'二级分类点开取消的验证')
                    // 删除三级分类对应的没有点开传值
                    couponTopic.thirdNoClickIds = couponTopic.thirdNoClickIds.filter(function (item) {
                        for (var key in item) {
                            return key != categoryId
                        }
                    });
                    // 删除三级分类点击的
                    couponTopic.thirdCategoryIds = couponTopic.thirdCategoryIds.filter(function (item) {
                        for (var key in item) {
                            // 删除四级分类的存储值
                            if (key == categoryId) {
                                threeItem = item[key];
                            }
                            return key != categoryId
                        }
                    });
                    couponTopic.categoryIds = couponTopic.categoryIds.filter(function (item) {
                        for (var key in item) {
                            return key != threeItem;
                        }
                    });
                    couponTopic.secondCancelChecked.push(categoryId);
                    break;
                case 3:
                    // 删除三级点开的
                    couponTopic.thirdCategoryIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                if (item[key].indexOf(categoryId) > -1) {
                                    item[key].splice(item[key].indexOf(categoryId), 1)
                                }
                            }
                        }
                    });
                    //console.log(couponTopic.thirdCategoryIds,'删除三级点开的验证')
                    // 删除三级没有点开的值
                    couponTopic.thirdNoClickIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                item[key].splice(item[key].indexOf(categoryId), 1)
                            }
                        }
                    });
                    // 删除四级分类没有点开传值
                    couponTopic.categoryIds = couponTopic.categoryIds.filter(function (item) {
                        for (var key in item) {
                            return key != categoryId;
                        }
                    });
                    couponTopic.thirdCancelChecked.push(categoryId);
                    break;
                case 4:
                    // 删除四级没有点开
                    couponTopic.categoryIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                /* if(item[key].indexOf(categoryId)){}*/
                                item[key].splice(item[key].indexOf(categoryId), 1)
                            }
                        }
                    });
                    // console.log(couponTopic.categoryIds,'删除四级存储的验证')
                    couponTopic.fourthCancelChecked.push(categoryId);
                    break;
                default:
                    return ''

            }
        } else {
            switch (level) {
                case 1:
                    couponTopic.firstCancelChecked = couponTopic.firstCancelChecked.filter(function (item) {
                        return item != categoryId;
                    });
                    break;
                case 2:
                    // 添加二级分类没有点开传的值
                    couponTopic.secondNoClickIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                item[key].push(categoryId)
                            }
                        }
                    });
                    couponTopic.secondCancelChecked = couponTopic.secondCancelChecked.filter(function (item) {
                        return item != categoryId
                    });
                    break;
                case 3:
                    // 添加三级没有点开的值
                    couponTopic.thirdNoClickIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                item[key].push(categoryId)
                            }
                        }
                    });
                    couponTopic.thirdCancelChecked = couponTopic.thirdCancelChecked.filter(function (item) {
                        return item != categoryId
                    });
                    break;
                case 4:
                    // 添加末级没有点开的数据
                    couponTopic.fourthCancelChecked = couponTopic.fourthCancelChecked.filter(function (item) {
                        return item != categoryId
                    });
                    // 选择末级操作
                    couponTopic.categoryIds.forEach(function (item, index) {
                        for (var key in item) {
                            if (key == parentUnit) {
                                if (item[key].indexOf(categoryId) < 0) {
                                    item[key].push(categoryId)
                                }
                            }
                        }
                    });
                    break;
                default:
                    return ''

            }
        }
    },
    // 渲染分类回显数据
    renderSelectInput: function (ele, level, flag) {
        var inputChildren = $("#" + ele + "").children().find('input');
        if (level == 1) {
            if (couponTopic.firstCancelChecked.length <= 0) {
                return false
            }
            $.each(inputChildren, function (index, item) {
                var categoryId = $(item).val();
                if (flag) {
                    $(item).attr('disabled', 'disabled');
                    if (couponTopic.firstCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', true)
                    } else {
                        $(item).prop('checked', false)
                    }
                } else {
                    if (couponTopic.firstCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', false)
                    } else {
                        $(item).prop('checked', true)
                    }
                }

            })
        } else if (level == 2) {
            if (couponTopic.secondCancelChecked.length <= 0) {
                return false
            }
            $.each(inputChildren, function (index, item) {
                var categoryId = $(item).val();
                var parentUnit = $(item).attr('data-parentuuid');
                var ary = [];
                if (flag) {
                    $(item).attr('disabled', 'disabled');
                    if (couponTopic.secondCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', true)
                    } else {
                        $(item).prop('checked', false)
                    }
                } else {
                    couponTopic.secondCancel.forEach(function(item2){
                        for(var key in item2){
                            if(key == parentUnit && item2[key].indexOf(categoryId) > -1){
                                $(item).prop('checked', false)
                            }
                        }
                    })
                /*    if (couponTopic.secondCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', false)
                    } else {
                        $(item).prop('checked', true)
                    }*/
                }

            })
        } else if (level == 3) {
            if (couponTopic.thirdCancelChecked.length <= 0) {
                return false
            }
            $.each(inputChildren, function (index, item) {
                var categoryId = $(item).val();
                var parentUnit = $(item).attr('data-parentuuid');
                if (flag) {
                    $(item).attr('disabled', 'disabled');
                    if (couponTopic.thirdCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', true)
                    } else {
                        $(item).prop('checked', false)
                    }
                } else {
                    couponTopic.thirdCancel.forEach(function(item2){
                        for(var key in item2){
                            if(key == parentUnit && item2[key].indexOf(categoryId) > -1){
                                $(item).prop('checked', false)
                            }
                        }
                    })
                /*    if (couponTopic.thirdCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', false)
                    } else {
                        $(item).prop('checked', true)
                    }*/
                }

            })
        } else {
            if (couponTopic.fourthCancelChecked.length <= 0) {
                return false
            }
            $.each(inputChildren, function (index, item) {
                var categoryId = $(item).val();
                var parentUnit = $(item).attr('data-parentuuid');
                if (flag) {
                    $(item).attr('disabled', 'disabled');
                    if (couponTopic.fourthCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', true)
                    } else {
                        $(item).prop('checked', false)
                    }
                } else {
                    couponTopic.fourthCancel.forEach(function(item2){
                        for(var key in item2){
                            if(key == parentUnit && item2[key].indexOf(categoryId) > -1){
                                $(item).prop('checked', false)
                            }
                        }
                    })
                /*    if (couponTopic.fourthCancelChecked.indexOf(categoryId) > -1) {
                        $(item).prop('checked', false)
                    } else {
                        $(item).prop('checked', true)
                    }*/
                }
            })
        }
    },
    // 获取品牌
    getBrandInfo: function (brandName) {
        var params = {
            brandName: brandName
        }
        var successCallBack = function (data) {
            if (data.retStatus == 1) {
                var result = data.retData ? data.retData : [];
                couponTopic.renderBrand(result)
            } else {
                layer.msg(data.retMessage, {offset: 't'})
            }
        }
        return web.doAjax(root.goodsInter.goods_getProductBrand, 'post', params, successCallBack, 1);
    },
    renderBrand: function (data) {
        var str = ''
        if (data.length > 0) {
            $.each(data, function (index, item) {
                str += " <li class='clearfix pl-10 mb-5' >" +
                    "          <input data-brandName='" + item.brandName + "' class='input-checked' value='" + item.uuid + "' type='checkbox' style='margin-right: 10px'>" + item.brandName + "" +
                    "    </li>"
            })
        } else {
            str = '<li class="clearfix pl-10 mb-5">无数据</li>'
        }

        $('.brand-lists').html(str)
    },
    // 渲染已选品牌
    renderSelectedBrand: function (data) {
        var str = '';
        $.each(data, function (index, item) {
            str += '<li style="float: left;height: 22px;padding: 4px 10px;line-height: 22px;background-color: #4A90E2;color: #FFFFFF;border-radius: 4px;cursor: pointer;margin-right: 8px">' +
                '                  <span>' + item.name + '</span>' +
                '                   <span class="cancel-brand" data-id="' + item.id + '"> x </span>' +
                '</li>'
        });
        $('#selected-brand-lists').html(str)
    },
    //分类文字点击触发事件
    clickClassifyNameFn: function (eventTarget, levelNum) {
        var obj = {};
        // 判断input框是否被选中
        var inputElement = $(eventTarget).prev();
        var inputCheck = $(inputElement).is(':checked');
        var categoryId = $(eventTarget).attr('data-id');
        var parentUnit = $(eventTarget).attr('data-parentuuid');
        var level = $(eventTarget).attr('data-level');
        obj.categoryId = categoryId;
        obj.level = level;
        if (!inputCheck) {
            if (levelNum == 1) { // 一级分类特殊处理
                obj.categoryId = $(eventTarget).val()
                return obj
            } else {
                return false
            }

        } else {
            if (levelNum == 2) {
                //couponTopic.clickIdsFn(parentUnit, categoryId, couponTopic.secondCategoryIds);
                couponTopic.cancelFn(parentUnit, categoryId, couponTopic.secondCategoryIds);
                couponTopic.secondNoClickIds.forEach(function (item, index) {
                    for (var key in item) {
                        if (key == parentUnit) {
                            if (item[key].indexOf(categoryId) > -1) {
                                item[key].splice(item[key].indexOf(categoryId), 1)
                            }
                        }
                    }
                });
                // console.log(couponTopic.secondCategoryIds,'二级分类点击的验证')
            } else if (levelNum == 3) {
                //couponTopic.clickIdsFn(parentUnit, categoryId, couponTopic.thirdCategoryIds);
                couponTopic.cancelFn(parentUnit, categoryId, couponTopic.thirdCategoryIds);
                couponTopic.thirdNoClickIds.forEach(function (item, index) {
                    for (var key in item) {
                        if (key == parentUnit) {
                            if (item[key].indexOf(categoryId) > -1) {
                                item[key].splice(item[key].indexOf(categoryId), 1)
                            }
                        }
                    }
                })
                //console.log(couponTopic.thirdCategoryIds, '三级分类点击的')
            }
            return obj;
        }
    },
    //保存参数的处理
    /**
     *
     *
     * @param handleAry 处理的参数
     */
    handleParamsFn: function (handleAry) {
        var paramsIds = [];
        $.each(handleAry, function (index, item) {
            for (var key in item) {
                paramsIds = paramsIds.concat(item[key])
            }
        });
        /*return paramsIds.join(',')*/
        var newParamsIds = Array.from(new Set(paramsIds))
        return newParamsIds.join(',')
    },
    // 保存
    saveInfo: function (params) {
        var successCallBack = function (data) {
            if (data.errorCode == 0) {
                layer.msg('保存成功', {offset: 't'});
                window.parent.channel();// 刷新父页面接口
                parent.layer.closeAll();// 关闭父页面
            } else {
                layer.msg(data.msg, {offset: 't'})
            }
        }
        return web.doAjax(root.dockingplatform.insertChannelProductRule, 'post', params, successCallBack, 1);
    }
}
couponTopic.init()